package com.dusk.net
{
    import com.dusk.game.Logger;
    
    import flash.events.Event;
    
    public class ThreadVO
    {
        private static var _id:uint = 1;
        private var _id:uint = ThreadVO._id++;
        private var _assetLoader:AssetLoader;
        
        private var _onCompleteFuncList:Array = [];
        private var _onProgressFuncList:Array = [];
        private var _onIOErrorFuncList:Array = [];
        private var _readyLoadAssetList:Array = [];
        
        public function ThreadVO()
        {
            Logger.log("Create Loading Thread: " + _id);
        }
        
        public function setCallBacks(onComplete:Array, onProgress:Array, onIOErr:Array):void
        {
            _onCompleteFuncList = onComplete;
            _onProgressFuncList = onProgress;
            _onIOErrorFuncList = onIOErr;
        }
        
        public function setAssetList(list:Array):void
        {
            _readyLoadAssetList = list;
        }
        
        public function onUpdateProgress(evt:Event):void
        {
            for each (var func:Function in _onProgressFuncList)
            {
                func.call(null, _assetLoader.loadingInfo);
            }
        }
        
        private function onLoadQueueComplete(evt:Event):void
        {
            for each (var func:Function in _onCompleteFuncList)
            {
                func.call();
            }
            destroy();
        }
        
        private function onError(evt:Event):void
        {
            for each (var func:Function in _onIOErrorFuncList)
            {
                func.call();
            }
            destroy();
        }
        
        public function start():void
        {
            if(_readyLoadAssetList.length == 0)
            {
                onLoadQueueComplete(null);
                return;
            }
            _assetLoader = new AssetLoader();
            _assetLoader.addEventListener(AssetLoader.LOAD_FAILED, onError);
            _assetLoader.addEventListener(AssetLoader.UPDATE_PROGRESS, onUpdateProgress);
            _assetLoader.addEventListener(AssetLoader.ALL_LOAD_COMPLETE, onLoadQueueComplete);
            for each (var i:AssetItem in _readyLoadAssetList)
            {
                _assetLoader.addItem(i);
            }
        }
        
        private function destroy():void
        {
            if(_assetLoader)
            {
                _assetLoader.removeEventListener(AssetLoader.LOAD_FAILED, onError);
                _assetLoader.removeEventListener(AssetLoader.UPDATE_PROGRESS, onUpdateProgress);
                _assetLoader.removeEventListener(AssetLoader.ALL_LOAD_COMPLETE, onLoadQueueComplete);
                _assetLoader.destroy();
                _assetLoader = null;
            }
            _readyLoadAssetList = null;
            _onCompleteFuncList = null;
            _onProgressFuncList = null;
            _onIOErrorFuncList = null;
            Logger.log("Destroy Loading Thread: " + _id)
        }
    }
}
